//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using OpenLS.Chart.Internals;

namespace OpenLS.Chart.UI
{
    internal class StackedChartLayout <T> : ChartLayout where T: ChartStack, new()
    {
        private readonly double max;
        private readonly int maxIndex;
        private readonly double min;
        protected Dictionary<int, T> stacks = new Dictionary<int, T>();
        public override double GetZ(int seriesIndex)
        {
            return 0;
        }
        public StackedChartLayout(IEnumerable series)
        {
            foreach (Series s in series)
            {
                foreach (NumericValue value in ((INumericValuesProvider)s).GetValues())
                {
                    T stack;
                    if (!stacks.TryGetValue(value.Index, out stack))
                    {
                        stack = new T();
                        stacks.Add(value.Index, stack);
                    }
                    stack.AddValue(value, s.Index);
                    maxIndex = Math.Max(maxIndex, value.Index);
                }
            }
            min = double.MaxValue;
            max = double.MinValue;
            foreach (T stack in stacks.Values)
            {
                min = Math.Min(min, stack.Minimum);
                max = Math.Max(max, stack.Maximum);
            }
        }

        #region INumericChartDisplayInformation Members

        public override int Count
        {
            get { return maxIndex + 1; }
        }


        public override Bounds GetBounds(int index, int seriesIndex)
        {
            return stacks[index].GetBounds(seriesIndex);
        }

        public override Bounds GetBounds(NumericValue value, Series series, double floor)
        {
            return GetBounds(value.Index, series.Index);
        }
        public override double GetValue(NumericValue value, Series series, double floor)
        {
            return stacks[value.Index].GetValue(series.Index);
        }

        #endregion

        internal override Bounds GetXBounds(Bounds xBounds, int seriesIndex, int seriesCount)
        {
            return xBounds;
        }
    }
}
